package com.bbx.taxi.client.DB;

import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 历史联系人
 *
 */
public class ContactsDB  implements DBcolumns{
	private DBHelper helper;
	public ContactsDB(Context context) {
		helper = DBHelper.getInstance(context,null);
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 *查询
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_CONTACTS, null, null, null, null,
				null, CONTACTS_ID + " desc");
		return cursor;

	}
	/**
	 *查询收藏或者历史
	 */
	public Cursor onQuery(String isCollection) {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_CONTACTS, null, CONTACTS_COLLECTION+"='"+isCollection+"'", null, null,
				null, CONTACTS_ID + " desc");
		return cursor;

	}
	/**
	 *查询是否有这个字段,
	 */
	public boolean onQuery(String name,String tel,String isCollection) {
		name=name.replace("'", "''");
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_CONTACTS, null, CONTACTS_NAME+"='"+name+"' and "+CONTACTS_TEL+"='"+tel+"' and "+CONTACTS_COLLECTION+"='"+isCollection+"'", null, null,
				null, CONTACTS_ID + " desc");
		if(cursor.getCount()>0)
			return true;
		return false;

	}
	/**
	 * 删除
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_CONTACTS, null, null);
		SQLdb.close();
	}
	/**
	 * 根据收藏或历史进行删除
	 */
	public void onDeleteType(String isCollection) {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_CONTACTS, CONTACTS_COLLECTION+"='"+isCollection+"'", null);
		SQLdb.close();
	}
	/**
	 * 根据收藏或历史进行删除
	 */
	public void onDelete(String rowId) {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_CONTACTS, CONTACTS_ID+"='"+rowId+"'", null);
		SQLdb.close();
	}
	/**
	 * 根据名字跟收藏进行删除
	 */
	public void onDelete(String name,String tel) {
		name=name.replace("'", "''");
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_CONTACTS, CONTACTS_NAME+"='"+name+"' and "+CONTACTS_TEL+"='"+tel+"'", null);
		SQLdb.close();
	}
	/**
	 *更新一条数据(更好顺序)
	 */
	public void onUpdate_sequence(String rowId, String  name,String tel,String collection) {
		onDelete(rowId);
		onWrite(name,tel,collection);
	}
	/**
	 *更新一条数据
	 */
	public void onUpdate(String rowId, String  name,String tel,String collection) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues cv = new ContentValues();
		cv.put(CONTACTS_NAME, name);
		cv.put(CONTACTS_TEL, tel);
		cv.put(CONTACTS_COLLECTION, collection);
		SQLdb.update(TABLE_CONTACTS, cv, CONTACTS_ID + "=" + rowId, null);
		SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(String name, String tel,String collection) {
		//如果有这个字段且是收藏的话，则不经行删除也不存储
		if(onQuery(name,tel,"1")){
			return;
		}
		//删除同个记录
		onDelete(name,tel);
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(CONTACTS_NAME, name);
		values.put(CONTACTS_TEL, tel);
		values.put(CONTACTS_COLLECTION, collection);
		SQLdb.insert(TABLE_CONTACTS, CONTACTS_ID, values);
		SQLdb.close();
	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
